<template>
  <el-card>
    <div class="box" v-if="$route.query.id">
  <el-breadcrumb separator-class="el-icon-arrow-right">
  <el-breadcrumb-item :to="{ path: '/subjects/list' }">学科管理</el-breadcrumb-item>
  <el-breadcrumb-item>{{$route.query.name}}</el-breadcrumb-item>
  <el-breadcrumb-item>标签管理</el-breadcrumb-item>
  </el-breadcrumb>
    </div>
    <el-row type="flex" justify="space-between" class="selectForm">
      <!-- 左侧的搜索表单 -->
      <el-col :span="20">
      <!-- 表单区域 -->
      <el-form  ref="searchRef" label-width="80px" inline size="small" :model="ed">
        <el-form-item label="标签名称">
          <el-input v-model.trim="ed.tagName"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-select placeholder="请选择" v-model="state">
            <el-option label="已启用" value='1'></el-option>
            <el-option label="已禁用" value='0'></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="info" @click="clear">清除</el-button>
          <el-button type="primary" @click="search">搜索</el-button>
        </el-form-item>
      </el-form>
      </el-col>
      <!-- 右侧发表按钮 -->
      <el-col :span="2">
         <el-link v-if="subjectID" :underline="false" type="success" @click="$router.back()"><i class="el-icon-back"></i> 返回学科</el-link>
        <el-button type="success" size="small" @click="openadd"><i class="el-icon-edit"></i>新增标签</el-button>
      </el-col>
    </el-row>
    <div class="gray"><i class="el-icon-info"></i> 数据一共 {{total}} 条</div>
     <!-- 表格 -->
    <el-table border stripe :data="list" style="margin-bottom:10px">
      <el-table-column label="序号" type="index" :index="indexMethod"></el-table-column>
      <el-table-column label="所属学科" prop="subjectName"></el-table-column>
      <el-table-column label="标签名称" prop="tagName"></el-table-column>
      <el-table-column label="创建者" prop="username"></el-table-column>
      <el-table-column label="创建日期">
        <template #default={row}>
          {{row.addDate | parseTime}}
        </template>
      </el-table-column>
      <el-table-column label="状态">
         <template #default={row}>
           {{row.state===1?'已启用':'已禁用'}}
         </template>
       </el-table-column>
     <el-table-column width="240px" label="操作" fixed="right">
         <template #default={row}>
            <el-button size="small" type="success" @click="stateChange(row)">{{row.state? '禁用' : '启用'}}</el-button>
            <el-button size="small" :disabled=Boolean(row.state) type="warning" @click="checkChange(row)">修改</el-button>
            <el-button size="small" :disabled=Boolean(row.state) type="danger" @click="del(row)">删除</el-button>
         </template>
       </el-table-column>
    </el-table>
     <!-- 分页 -->
    <el-pagination
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="ed.page"
      :page-sizes="[10,20,30,40]"
      :page-size="ed.pagesize"
      layout="prev, pager, next,sizes, jumper"
      :total="total">
    </el-pagination>
     <!-- 对话框 -->
  <TagsAdd :dialogVisible.sync='dialogVisible' :list='list' @again='againFn' ref="refAdd" :subjectID='subjectID' @agagintwo='agagintwo'></TagsAdd>
  </el-card>
</template>

<script>
import { list, changeState, remove, detail } from '@/api/hmmm/tags.js'
import TagsAdd from '@/module-hmmm/components/tags-add'
export default {
  name: 'Tags',
  data () {
    return {
      list: [], // 标签列表
      total: 0,
      ed: {
        page: 1,
        pagesize: 10,
        tagName: null
      },
      state: null,
      dialogVisible: false,
      subjectID: ''
    }
  },
  components: {
    TagsAdd
  },
  methods: {
    // 获取标签列表
    async getList () {
      const { data } = await list({ ...this.ed, state: this.state })
      console.log(data)
      this.list = data.items
      this.total = data.counts
    },
    // 自定义索引
    indexMethod (index) {
      return index + 1 + (this.ed.page - 1) * this.ed.pagesize
    },
    // 切换状态
    async stateChange (row) {
      if (row.state === 1) {
        await changeState({ ...row, state: '0' })
        !this.subjectID ? this.getList() : this.getIDList()
      }
      if (row.state === 0) {
        await changeState({ ...row, state: '1' })
        !this.subjectID ? this.getList() : this.getIDList()
      }
    },
    // 搜索
    async search () {
      if (!this.ed.tagName && !this.state) return
      if (this.subjectID) {
        const { data } = await list({ ...this.ed, state: this.state, subjectID: this.subjectID })
        this.list = data.items
        this.total = data.counts
      } else {
        const { data } = await list({ ...this.ed, state: this.state })
        this.list = data.items
        this.total = data.counts
      }
    },
    // 清除
    clear () {
      if (this.subjectID) {
        this.getIDList()
        this.ed.tagName = null
        this.state = null
      } else {
        this.ed.page = 1
        this.ed.pagesize = 10
        this.ed.tagName = null
        this.state = null
        this.getList()
      }
    },
    // pageSize 改变时会触发
    handleSizeChange (val) {
      this.ed.pagesize = val
      this.getList()
    },
    // currentPage 改变时会触发
    handleCurrentChange (val) {
      this.ed.page = val
      this.getList()
    },
    // 删除标签
    del (row) {
      this.$confirm('此操作将永久删除该目录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        // 1. 发送请求
        await remove(row)
        this.$message.success('删除成功')
        // 2.当页面条数只剩一条数据并且页码大于1时，删除后自动跳转到前一页
        if (this.list.length === 1 && this.ed.page > 1) {
          this.ed.page--
        }
        // 3.重新渲染
        // this.getList()
        !this.subjectID ? this.getList() : this.getIDList()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 点击新增标签
    openadd () {
      this.dialogVisible = true
      this.$refs.refAdd.title = '新增标签'
    },
    // 重新渲染，接受子组件请求
    againFn () {
      this.getList()
    },
    // 点击修改标签
    async checkChange (row) {
      // 发送请求，获取点击这一项的数据
      const { data } = await detail(row)
      this.$refs.refAdd.w = data
      this.dialogVisible = true
      this.$refs.refAdd.title = '修改标签'
    },
    // 获取从学科页面点击的列表
    async getIDList () {
      if (!this.subjectID) return
      const { data } = await list({ subjectID: this.subjectID })
      this.list = data.items
      this.total = data.counts
    },
    agagintwo () {
      this.getIDList()
    }
  },
  async created () {
    await this.getList()
    this.subjectID = this.$route.query.id
    await this.getIDList()
  }

}
</script>

<style scoped lang='scss'>
.gray {
  padding-left: 10px;
  margin: 10px 0;
  width: 100%;
  height: 40px;
  line-height: 40px;
  background-color:#ccc;
}
::v-deep .el-pagination.is-background {
 flex: 1;
   text-align: right;
}
.el-link {
  transform: translateY(20px) translateX(-100px);
}
.box {
  width: 100%;
  margin-bottom: 10px;
  height: 40px;
  border-bottom: 1px solid #ccc;
}
::v-deep .el-breadcrumb__inner.is-link {
  color: #333 !important;
}
</style>
